package net.huashitong.util;

import com.sirdc.modules.utils.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.SimpleFormatter;
import java.util.regex.Pattern;

public class DateUtil {

    private static String DATE_REGEX = "^([1-9]\\d{3}-)(([0]{0,1}[1-9]-)|([1][0-2]-))(([0-3]{0,1}[0-9]))$";


    //下个季度的同一天
    public static String  nextQuarterDay(String yyyymmdd) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date  = sdf.parse(yyyymmdd);
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.add(Calendar.MONTH,3);//增加三个月
        Date newDate = ca.getTime();
        String format = sdf.format(newDate);
       // System.out.println("增加3个月后："+format);

        return format;
    }


    public static  String getCurrentMonth(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String currentDate = sdf.format(new Date());
        return currentDate;
    }


    /**
     * 获取当前日期
     * @return
     */
    public static  String getCurrentDate(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String currentDate = sdf.format(new Date());
        return currentDate;
    }

    public static  String getCurrentTime(){
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        String currentDate = sdf.format(new Date());
        return currentDate;
    }


    public static Date strToDate(String datestr){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date parse = sdf.parse(datestr);


            return parse;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String DateToStr(Date date)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);
        return format;

    }
    public static String dateFormat(String date,String pattern){
        if(StringUtils.isBlank(pattern)){
            return  date;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date parse = null;
        try {
            parse = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String format = sdf.format(parse);
        return  format;
    }

    //根据周期性计算 日期；
    public static String getRemindDate(String period,String day,String week,String month,String quarter){
        if(StringUtils.isBlank(period))
        {
            return "error";
        }
        String remindDate ="";
        Calendar ca = Calendar.getInstance();
        ca.setTime(new Date());
        String cycleValue = "";
        switch (period){
            case "0"://天
                cycleValue=day;
                int addDay = Integer.parseInt(cycleValue);
                ca.add(Calendar.DATE,addDay);
                remindDate =DateUtil.DateToStr(ca.getTime());

                break;
            case "1"://周
                cycleValue=week;
                int weeks = ca.get(Calendar.DAY_OF_WEEK);
                int wk =Integer.parseInt(cycleValue);
                wk=wk+1;
                int abs = Math.abs(weeks - wk);
                ca.add(Calendar.DATE,abs);
                remindDate = DateUtil.DateToStr(ca.getTime());
                break;

            case "2"://月
                cycleValue=month;
                int currDay = ca.get(Calendar.DAY_OF_MONTH);
                int temDay =Integer.parseInt(cycleValue);

                String currentMonth = DateUtil.getCurrentMonth();

                if (currDay-temDay>0) {
                    //提醒日期在  早于 今天，提醒日期设置为下个月的
                    ca.add(Calendar.MONTH,1);
                    ca.getTime();
                    int maximum = ca.getActualMaximum(Calendar.DAY_OF_MONTH);
                    int year = ca.get(Calendar.YEAR);
                    int month2 =ca.get(Calendar.MONTH)+1;
                    if(maximum-temDay<=0){//考虑2月情况
                        remindDate=year+"-"+month2+"-"+maximum;
                    }else {
                        remindDate=year+"-"+month2+"-"+temDay;
                    }
                }else {
                    remindDate= currentMonth+"-"+temDay;
                }
                break;
            case "3"://季度
                cycleValue=quarter;
                Date alertDate = DateUtil.strToDate(cycleValue);
                Date currDate = DateUtil.strToDate(DateUtil.getCurrentDate());
                if (alertDate.compareTo(currDate)>0) {
                    //提醒日期 是否晚于 今天 如果晚于的话，直接设置。
                    remindDate = cycleValue;
                }else {
                    //下一个季度
                    ca.setTime(alertDate);
                    ca.add(Calendar.MONTH,3);
                    remindDate = DateUtil.DateToStr(ca.getTime());
                }
                break;

        }
        remindDate= dateFormat(remindDate,"yyyy-MM-dd");
        return  remindDate;
    }


    /**
     * 周期新自动计算下轮时间
     * @param nowDate
     * @param period
     * @param day
     * @return
     */
    public static String nextRemindDate(String nowDate,String period,String day){



        String remindDate ="";
        Calendar ca = Calendar.getInstance();
        ca.setTime(strToDate(nowDate));
        String cycleValue = "";

        switch (period){
            case "0"://天
                cycleValue=day;
                int addDay = Integer.parseInt(cycleValue);
                ca.add(Calendar.DATE,addDay);
                remindDate =DateUtil.DateToStr(ca.getTime());

                break;
            case "1"://周

                ca.add(Calendar.DATE,7);
                remindDate =  DateUtil.DateToStr(ca.getTime());
                break;

            case "2"://月
                ca.add(Calendar.MONTH,1);
                remindDate =DateUtil.DateToStr(ca.getTime());
                break;
            case "3"://季度
                ca.add(Calendar.MONTH,3);
                remindDate =DateUtil.DateToStr(ca.getTime());
                break;

        }

       remindDate = dateFormat(remindDate,"yyyy-MM-dd");
        return remindDate;

    }

    /**
     * 多时限计算下轮时间
     * @param Dates
     * @param currentDate
     * @return
     */
    public static String nextDateByMultiTime(String [] Dates ,String currentDate){
        String nextDate= "";
        for (int i = 0; i < Dates.length; i++) {
            if(currentDate.equals(Dates[i])){
                if(i!=(Dates.length-1)){

                    nextDate=Dates[i+1];
                    break;
                }else {
                    return  "";
                }
            }


        }

        return nextDate;
    }

    public static int weekToInt(String weeks){
        weeks=weeks.trim();
        switch (weeks){
            case "星期一":
                return 1;
            case "星期二":
                return 2;
            case "星期三":
                return 3;
            case "星期四":
                return 4;
            case "星期五":
                return 5;
            case "星期六":
                return 6;
            case "星期天":
                return 7;
            default:
                return 0;
        }
    }

    public static String IntToWeek(String StringInt){
        StringInt=StringInt.trim();
        switch (StringInt){
            case "1":
                return "星期一";
            case "2":
                return "星期二";
            case "3":
                return "星期二";
            case "4":
                return "星期四";
            case "5":
                return "星期五";
            case "6":
                return "星期六";
            case "7":
                return "星期天";
            default:
                return "0";
        }
    }

    /***
     * @desc 校验日期的格式，yyyy-MM-dd，无法校验dd的完整性，
     * 就是可能出现 2020-2-32，2020-1-33这样的天数，可以通过设置日期的严禁性来转成日期，若报错则日期不正确
     * @author fanzhen
     * @date 2020-05-28 18:50
     * @param
     * @param datestr：日期，格式：yyyy-MM-dd
     * @return boolean
     */
    public static boolean validDateEffecitive(String datestr) throws Exception{
        boolean matches =  Pattern.matches(DATE_REGEX, datestr);
        if(!matches){
            return matches;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //设置日期格式转的严谨性
        sdf.setLenient(false);
        try {
            sdf.parse(datestr);
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
        return matches;
    }



}
